﻿using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace AnesSystem.ViewModels
{
    public class VerticalMenuCtrlViewModel:ViewModel
    {
        #region Variables

        private ObservableCollection<ToggleButton> _toggleBtnList = new ObservableCollection<ToggleButton>();
        private ToggleButton _preTlb;
        private bool isMouseClick = false;

        #endregion

        #region Constructor

        public VerticalMenuCtrlViewModel()
        {

        }

        #endregion

        #region Properties

        public ObservableCollection<ToggleButton> MenuCtrlList
        {
            get
            {
                return _toggleBtnList;
            }
            set
            {
                _toggleBtnList = value;
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        public event EventHandler<MenuListModel> Click;

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        public void DispalyLeftMenu(int pId, string tableName = "")
        {
            MenuCtrlList.Clear();
            var plist = DataManager.Instance.MenuList.Where(O => O.Parent == pId).OrderBy(O => O.Order);
            ToggleButton selBtn = null;
            foreach (var item in plist)
            {
                ToggleButton btn = new ToggleButton();
                btn.Content = item.Name;
                btn.DataContext = item;
                if (item.ClassName.Equals(tableName))
                {
                    selBtn = btn;
                }
                MenuCtrlList.Add(btn);
            }
            if (selBtn != null)
            {
                selBtn.Unchecked -= ToggleBtn_CheckChanged;
                selBtn.Checked -= ToggleBtn_CheckChanged;
                selBtn.IsChecked = true;
                SetBtnState(selBtn);
                selBtn.Unchecked += ToggleBtn_CheckChanged;
                selBtn.Checked += ToggleBtn_CheckChanged;
            }
        }

        #endregion

        #region EventHandlers

        private void LeftMenuItemClick(MenuListModel model)
        {
            isMouseClick = true;
            Click?.Invoke(this, model);
        }

        private void ToggleBtn_CheckChanged(object sender, RoutedEventArgs e)
        {
            ToggleButton btn = sender as ToggleButton;
            SetBtnState(btn);
            LeftMenuItemClick(btn.DataContext as MenuListModel);
        }

        private void SetBtnState(ToggleButton btn)
        {
            if (_preTlb != btn)
            {
                foreach (ToggleButton ctrl in _toggleBtnList)
                {
                    if (!btn.Equals(ctrl))
                    {
                        ctrl.Unchecked -= ToggleBtn_CheckChanged;
                        ctrl.Checked -= ToggleBtn_CheckChanged;
                        ctrl.IsChecked = !btn.IsChecked;
                        ctrl.Unchecked += ToggleBtn_CheckChanged;
                        ctrl.Checked += ToggleBtn_CheckChanged;
                    }
                }
                _preTlb = btn;
            }
            else
            {
                btn.Unchecked -= ToggleBtn_CheckChanged;
                btn.Checked -= ToggleBtn_CheckChanged;
                btn.IsChecked = !btn.IsChecked;
                btn.Unchecked += ToggleBtn_CheckChanged;
                btn.Checked += ToggleBtn_CheckChanged;
            }
        }

        #endregion
    }
}
